package com.jrx.anytxn.param.service.aviator.impl;

import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.aviator.AviatorDicInfoReq;
import com.jrx.anytxn.param.dto.aviator.AviatorDicInfoRes;
import com.jrx.anytxn.param.dto.aviator.TransactionDicInfoRes;
import com.jrx.anytxn.param.entity.RmAviatorDicInfo;
import com.jrx.anytxn.param.mapper.ext.ExtRmAviatorDicInfoMapper;
import com.jrx.anytxn.param.service.aviator.IDicInfoService;
import com.jrx.anytxn.param.utils.TransactionDicInfoFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Qingyao Xing
 * @date 2019/7/12 17:15
 */
@Service
public class DicInfoServiceImpl implements IDicInfoService {

    private static final Logger logger=LoggerFactory.getLogger(DicInfoServiceImpl.class);

    @Resource
    private ExtRmAviatorDicInfoMapper extRmAviatorDicInfoMapper;

    @Autowired
    private ISegmentService segmentService;

    /**
     * 根据主键获取该字典数据
     */
    @Override
    public AviatorDicInfoRes getDicByPrimaryKey(Long id,String tenantId) {

        return BeanMapping.copy(extRmAviatorDicInfoMapper.selectByIdAndTenId(id,tenantId), AviatorDicInfoRes.class);
    }

    /**
     * 新增字典数据
     */
    @Override
    public Boolean registerAviatorDicInfo(AviatorDicInfoReq aviatorDicInfoReq)throws TxnBizException {
        RmAviatorDicInfo aviatorDicInfo = BeanMapping.copy(aviatorDicInfoReq, RmAviatorDicInfo.class);
        try {
            aviatorDicInfo.setId(segmentService.getId("RmAviatorDicInfo"));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
        }
        aviatorDicInfo.setRecordVersionNumber(Constant.VERSION_NUMBER);
        int i = extRmAviatorDicInfoMapper.insertSelective(aviatorDicInfo);
        return i > 0;


    }

    /**
     * 修改字典数据
     */
    @Override
    public Boolean modifyAviatorDicInfo(AviatorDicInfoReq aviatorDicInfoReq) {
        RmAviatorDicInfo aviatorDicInfo = BeanMapping.copy(aviatorDicInfoReq, RmAviatorDicInfo.class);
        int i = extRmAviatorDicInfoMapper.updateByIdAndTenId(aviatorDicInfo);
        return i > 0;
    }

    /**
     * 根据主键删除字典、
     */
    @Override
    public Boolean cancelAviatorDicInfo(Long id,String tenantId) {
        int i = extRmAviatorDicInfoMapper.deleteByIdAndTenId(id,tenantId);
        return i > 0;

    }

    /**
     * 根据 规则类型 和组织ID查询规则字典集合
     *
     * @param ruleType
     * @param tenantId
     * @return
     */
    @Override
    public List<AviatorDicInfoRes> getAllDicByParams(String ruleType, String tenantId) {
        List<AviatorDicInfoRes> list = null;
        List<RmAviatorDicInfo> dicInfoList = extRmAviatorDicInfoMapper.selectAllDicByParams(ruleType, tenantId);
        if (!CollectionUtils.isEmpty(dicInfoList)) {
            list = BeanMapping.copyList(dicInfoList, AviatorDicInfoRes.class);
        }
        return list;
    }

    @Override
    public List<TransactionDicInfoRes> getTransactionIdentificationParam() {
        return TransactionDicInfoFunction.builtTransactionDicInfoList();
    }
}
